var prefrofile=[]
var tbodys = document.querySelector("tbody")
var pageSize = 6; //每页显示的记录条数
var page = 1; //当前页，默认第一页

// 查询学生档案
function selectstu(){
    let stu={
        pageIndex:page,
        pageSize:pageSize,
        fileType:0
    }
    ajaxHttp("post","file/fileList",stu).then((res)=>{
        // console.log(res);
        res=JSON.parse(res)
       var data=res.dataset.fileList
       console.log(data);
       prefrofile=data
       for (var i = 0; i < prefrofile.length; i++) {
        var newtr = document.createElement("tr")
        newtr.innerHTML = `
             <td>${prefrofile[i].fileName}</td>
                            <td class="filesex">${prefrofile[i].fileSex}</td>
                            <td class="filecla">${prefrofile[i].fileClazz}</td>
                            <td>${prefrofile[i].fileDate}</td>
                            <td>${prefrofile[i].filePhone}</td>
                            <td>${prefrofile[i].fileAdress}</td>
                            <td>${prefrofile[i].sosPerson}</td>
                            <td>${prefrofile[i].sosPhone}</td>
                            <td class="operate">
                            <button class="revise" onclick="revise(${prefrofile[i].fileId})" style="color:#1BB27F; cursor:pointer;">修改</button>
                            <button onclick="deltr(${prefrofile[i].fileId})" style="color:#E86A73;cursor:pointer;">删除</button>
                        </td>
        `
        tbodys.appendChild(newtr)
    }
    filesex()
    file()
    })
}
selectstu()


// 判断学生状态
function file(){
    var file=document.querySelectorAll(".file")
    console.log(file);
    for(var i=0;i<file.length;i++){
        // console.log(file[i]);
        if(file[i].innerText==="0"){
            file[i].innerText="未毕业"
            file[i].style.color="green"
        }else if(file[i].innerText==="1"){
            file[i].innerText="已毕业"
            file[i].style.color="red"
        }
    }
}
//判断学生性别
function filesex(){
    var sex=document.querySelectorAll(".filesex")
    console.log(sex);
    for(var i=0;i<sex.length;i++){
        if(sex[i].innerText==="1"){
            sex[i].innerText="男"
        }else if(sex[i].innerText==="2"){
            sex[i].innerText="女"
        }
    }
}

// 按照名字查询学生
function selename(){
    var selname=document.querySelector("#selname")
    // console.log(selname);
    let sele={
        pageIndex:page,
        pageSize:pageSize,
        fileName:selname.value,
        fileType:0
    }
    ajaxHttp("post","file/fileList",sele).then((res)=>{
       // console.log(res);
       res=JSON.parse(res)
       var data=res.dataset.fileList
       console.log(data);
       prefrofile=data
       for (var i = 0; i < prefrofile.length; i++) {
        if(selname.value===prefrofile[i].fileName){
            tbodys.innerHTML=""
           for(var j=0;j<prefrofile.length;j++){
            var newtr = document.createElement("tr")
            newtr.innerHTML = `
                 <td>${prefrofile[i].fileName}</td>
                                <td class="filesex">${prefrofile[i].fileSex}</td>
                                <td class="filecla">${prefrofile[i].fileClazz}</td>
                                <td>${prefrofile[i].fileDate}</td>
                                <td>${prefrofile[i].filePhone}</td>
                                <td>${prefrofile[i].fileAdress}</td>
                                <td>${prefrofile[i].sosPerson}</td>
                                <td>${prefrofile[i].sosPhone}</td>
                                <td class="operate">
                                <button class="revise" onclick="revise()" style="color:#1BB27F; cursor:pointer;">修改</button>
                                <button onclick="deltr()" style="color:#E86A73;cursor:pointer;">删除</button>
                            </td>
            `
            tbodys.appendChild(newtr)
           }
        }
        
    }
    file()
    filesex()
    })
}

var opens=document.querySelector(".open")
// 单击修改打开修改弹窗
function revise(rev){
    var open=document.querySelector(".open")
    console.log(open);
    var names=open.querySelector(".names")
    var sex=open.querySelector(".sex")
    var tel=open.querySelector(".tel")
    var souser=open.querySelector(".souser")
    var clazz=open.querySelector(".clazz")
    var datetime=open.querySelector("#datetime")
    var adress=open.querySelector(".adress")
    var sophone=open.querySelector("#sophone")
    var pwd=open.querySelector(".pwd")
    opens.style.display="flex"
    // 在打开遮罩的时候获取到当前行的tr数据
    localStorage.setItem("fileid",rev)
    // console.log(this)
    var obs=this.prefrofile
    // console.log(obs);
    for(var i=0;i<obs.length;i++){
        // console.log(obs[i]);
        if(rev===obs[i].fileId){
            names.value=obs[i].fileName
            sex.value=obs[i].fileSex
            tel.value=obs[i].filePhone
            souser.value=obs[i].sosPerson
            clazz.value=obs[i].fileClazz
            datetime.value=obs[i].fileDate
            adress.value=obs[i].fileAdress
            sophone.value=obs[i].sosPhone
            pwd.value=obs[i].password
        }
    }
    // console.log(opsuer);
}

// 单击修改确定的时候调用接口
function revsure(){
    var open=document.querySelector(".open")
    console.log(open);
    var names=open.querySelector(".names")
    var sex=open.querySelector(".sex")
    var tel=open.querySelector(".tel")
    var souser=open.querySelector(".souser")
    var clazz=open.querySelector(".clazz")
    var datetime=open.querySelector("#datetime")
    var adress=open.querySelector(".adress")
    var sophone=open.querySelector("#sophone")
    var pwd=open.querySelector(".pwd")
    var num=localStorage.getItem("fileid")
    var fileid=Number(num)
    let revs={
        fileId:fileid,
        fileName:names.value,
        fileClazz:clazz.value,
        fileSex:sex.value,
        filePhone:tel.value,
        fileAdress:adress.value,
        SosPerson:souser.value,
        password:pwd.value,
        fileDate:datetime.value,
        SosPhone:sophone.value,
        fileType:0
        
    }
    ajaxHttp("post","file/updateFile",revs).then((res)=>{
        // console.log(res);
        alert("修改成功")
        offrev()
        location.reload()
    })
}

// 单击修改取消关闭遮罩
function offrev(){
    opens.style.display="none"
}



// 删除功能
var dels=document.querySelector(".del")
// console.log(dels);
function deltr(del){
    // 调用删除接口
    // console.log(del);
    localStorage.setItem("fileid",del)
    // 打开删除接口
   dels.style.display="flex"
}
// 取消关闭删除遮罩功能
function offdel(){
    dels.style.display="none"
}

// 调用删除接口删除当前学生数据
function delstud(){
    var index=localStorage.getItem("fileid")
    // console.log(index);
    var num=Number(index)
    let stud={
        fileId:num
    }
    ajaxHttp("post","file/deleteFile",stud).then((res)=>{
        alert("删除成功")
        offdel()
        location.reload();
    })
}



// 添加预招生接口
function addstud(){
    var addopen=document.querySelector(".addopen")
    var names=addopen.querySelector(".names")
    var sex=addopen.querySelector(".sex")
    var tel=addopen.querySelector(".tel")
    var souser=addopen.querySelector(".souser")
    var clazz=addopen.querySelector(".clazz")
    var datetime=addopen.querySelector("#datetime")
    var adress=addopen.querySelector(".adress")
    var sophone=addopen.querySelector("#sophone")
    var pwd=addopen.querySelector(".pwd")
    // console.log(sex.value);
    let stude={
        fileName:names.value,
        fileSex:sex.value,
        fileClazz:clazz.value,
        fileDate:datetime.value,
        filePhone:tel.value,
        fileAdress:adress.value,
        SosPerson:souser.value,
        SosPhone:sophone.value,
        fileType:0,
        password:pwd.value
    }
    if(names.value!=""&&sex.value!=""&&tel.value!=""&&souser!=""&&clazz!=""&&datetime.value!=""&&adress.value!=""&&sophone.value!=""){
        ajaxHttp("POST","file/addfile",stude).then((res)=>{
            alert("添加成功")
            offadd()
            location.reload();
        })
    }else{
        alert("表单内部数据不能有空")
    }
}
// 打开添加用户
var addopens=document.querySelector(".addopen")
function addopen(){
    addopens.style.display="flex"
}
function offadd(){
    addopens.style.display="none"
}


// 分页
var theUL = document.getElementById("pageListTableBody");
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum"); //获取当前页<span>
var spanPre = document.getElementById("spanPre"); //获取上一页<span>
var spanNext = document.getElementById("spanNext"); //获取下一页<span>
var spanFirst = document.getElementById("spanFirst"); //获取第一页<span>
var spanLast = document.getElementById("spanLast"); //获取最后一页<span>
var numberRowsInTable = theUL.getElementsByTagName("tr").length; //记录总条数

//下一页
function next() {
    hideTable();
    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if (maxRow > numberRowsInTable) maxRow = numberRowsInTable;
    for (var i = currentRow; i < maxRow; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    page++;
    if (maxRow == numberRowsInTable) {
        nextText();
        lastText();
    }
    showPage();
    preLink();
    firstLink();
}

//上一页
function pre() {
    hideTable();
    page--;
    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;
    if (currentRow > numberRowsInTable) currentRow = numberRowsInTable;
    for (var i = maxRow; i < currentRow; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    if (maxRow == 0) {
        preText();
        firstText();
    }
    showPage();
    nextLink();
    lastLink();
}

//第一页
function first() {
    hideTable();
    page = 1;
    for (var i = 0; i < pageSize; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    firstText();
    preText();
    nextLink();
    lastLink();
}

//最后一页
function last() {
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for (var i = currentRow; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    preLink();
    nextText();
    firstLink();
    lastText();
}

function hideTable() {
    for (var i = 0; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
}

function showPage() {
    pageNum.innerHTML = page;
}

//总共页数
function pageCount() {
    return Math.ceil(numberRowsInTable / pageSize);
}
//显示链接
function preLink() {
    spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";
}
function preText() {
    spanPre.innerHTML = "上一页";
}
function nextLink() {
    spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";
}
function nextText() {
    spanNext.innerHTML = "下一页";
}
function firstLink() {
    spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";
}
function firstText() {
    spanFirst.innerHTML = "首页";
}
function lastLink() {
    spanLast.innerHTML = "<a href='javascript:last();'>末页</a>";
}
function lastText() {
    spanLast.innerHTML = "末页";
}
//隐藏
function hide() {
    for (var i = pageSize; i < numberRowsInTable; i++) {
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    nextLink();
    lastLink();
}
hide();